<?php

class Application_Form_Event extends Zend_Form
{
	protected $_type;
	public function __construct($type=null){
		// close, add, edit
		$this->_type = $type;
		parent::__construct();		
	}
	
    public function init()
    {
        $this->setMethod('post');
        $closeElements = array();
        $addElements = array();
        $submitEl = new Zend_Form_Element_Submit('submit');
        if($this->_type == 'close' || $this->_type == 'edit'){        	
        	$closeElements[] = $scoreLeftEl = new Zend_Form_Element_Text('score_left');
        	$scoreLeftEl->setLabel('Home score')
        		->setOptions(array('size' => 5))
        		->setRequired(true)
        		->addValidator('Int')
        		->addValidator('GreaterThan', false, array('min' => -1));
        	$closeElements[] = $scoreRightEl = new Zend_Form_Element_Text('score_right');
        	$scoreRightEl->setLabel('Away score')
        		->setOptions(array('size' => 5))
        		->setRequired(true)
        		->addValidator('Int')
        		->addValidator('GreaterThan', false, array('min' => -1));
        	$submitEl->setLabel('Close');
        }
        if($this->_type == 'add' || $this->_type == 'edit'){	        
	        $addElements[] = $titleEl = new Zend_Form_Element_Text('title');
	        $titleEl->setLabel('Title')
	        		->setOptions(array('size' => 25))
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim');        		       		
	
	        $addElements[] = $descrEl = new Zend_Form_Element_Textarea('descr');
	        $descrEl->setLabel('Descr')
	        		->setOptions(array('rows' => 3, 'cols' => 50))
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim');
	        		
	        $addElements[] = $tournEl = new Zend_Form_Element_Select('tourn_id');
	        $tournEl->setLabel('Tournament')
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim')
	        		->addValidator('Int')
	        		->addValidator('GreaterThan', false, array('min' => 0));
	        		
	        $addElements[] = $leftEl = new Zend_Form_Element_Select('left_id');
	        $leftEl->setLabel('Home team')
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim')
	        		->addValidator('Int')
	        		->addValidator('GreaterThan', false, array('min' => 0));
	        		
	        $addElements[] = $rightEl = new Zend_Form_Element_Select('right_id');
	        $rightEl->setLabel('Away team')
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim')
	        		->addValidator('Int')
	        		->addValidator('GreaterThan', false, array('min' => 0));
	        // @Need to validate dates!
			/////////////////////////////////////////////
	        /*$addElements[] = $dateAddEl = new Zend_Form_Element_Text('date_add');
	        $dateAddEl->setLabel('Add date')
	        		->setOptions(array('size' => 25))
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim'); */
	        $addElements[] = $dateStartEl = new Zend_Form_Element_Text('date_start');
	        $dateStartEl->setLabel('Start date')
	        		->setOptions(array('size' => 25))
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim'); 	
	        $addElements[] = $dateCloseEl = new Zend_Form_Element_Text('date_close');
	        $dateCloseEl->setLabel('Close date')
	        		->setOptions(array('size' => 25))
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim');
	        /////////////////////////////////////////////        			
	
	
	        /*$elements[] = $resultEl = new Zend_Form_Element_Select('result');
	        $resultEl->setLabel('Event result')
	        		->addMultiOption(null, '-')
	                ->addMultiOption('left', 'left')
	        		->addMultiOption('right', 'right')
	        		->addMultiOption('draw', 'draw')
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim')
	        		->addValidator('Alnum');*/
	        		
	        $addElements[] = $statusEl = new Zend_Form_Element_Select('status');
	        $statusEl->setLabel('Event status')
	        		->addMultiOption('opened', 'opened')
	        		->addMultiOption('closed', 'closed')
	        		->addMultiOption('pending', 'pending')
	        		->setRequired(true)
	        		->addFilter('StripTags')
	        		->addFilter('StringTrim')
	        		->addValidator('Alnum');   
	        $addElements[] = $visibleEl = new Zend_Form_Element_Checkbox('visible');
	        $visibleEl->setLabel('Visible')
	        		->setRequired(true)
	        		->addValidator('Int');
	        $submitEl->setLabel('Add');
        }
        if($this->_type == 'edit'){
        	$scoreLeftEl->setRequired(false);
        	$scoreRightEl->setRequired(false);
        	$submitEl->setLabel('Edit');
        }    		
        $elements[] = $submitEl;

        $this->addElements($addElements);
        $this->addElements($closeElements);
        $this->addElement($submitEl);
    }
	public function fillForm(){
		$team = new Application_Model_Team();
		$teams = $team->getAll();
		foreach ($teams as $t){
			$this->left_id->addMultiOption($t['id'], $t['title']);
			$this->right_id->addMultiOption($t['id'], $t['title']);
		}
		$tourn = new Application_Model_Tournament();
		$tournaments = $tourn->getAll();
		foreach ($tournaments as $tourney){
			$this->tourn_id->addMultiOption($tourney['id'], $tourney['title']);
		}
		$this->date_start->setValue(date('Y-m-d H:i:s'));
		$this->date_close->setValue(date('Y-m-d H:i:s'));
	}
	public function saveForm($event_id = null){
		$event_tbl = new Application_Model_DbTable_Event();
		if($event_id){
			$event = $event_tbl->GetEventsById($event_id, null, null, null, null, false)->current();
			foreach($this->getValues() as $key => $value){
				$event->$key = $value;
			}
			//$event = $event->current();
		}
		else{
			$event = $event_tbl->createRow($this->getValues());
		}
		$event->save();
		//print_r($event->toArray());
	}
}

